package com.ray.quartz.controller;



import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ray.activiti.common.validate.ValidatorUtils;
import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.quartz.service.ScheduleJobService;
import com.ray.quartz.table.entity.ScheduleJob;
import com.ray.quartz.util.DataGridUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * 类ScheduleJobController的功能描述:
 * 定时任务
 * @auther ray
 * @date 2018-01-09 16:20:29
 */
@RestController
@RequestMapping("/quartz/task")
public class ScheduleJobController  extends WebController{
	 @Autowired
	 UcenterApiService ucenterApiService;
	 @Autowired
	 ScheduleJobService  scheduleJobService;
	
	  
	  
	 /**
     * 列表
     * @param model
     * @param actModelEntity
     * @param request
     * @return
     */
    @RequestMapping("view")
    @RequiresPermissions("quartz:task:view")
    public ModelAndView view(String menuId,ModelAndView modelAndView, HttpServletRequest request){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	modelAndView.setViewName("quartz/task/view");
        return modelAndView;
    }
    
    /**
     * 列表
     * @param model
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("query")
    @RequiresPermissions("quartz:task:query")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> query(Model model,ScheduleJob params, HttpServletRequest request){
    	LoginUser user = getLoginUser();
    	PageDto<ScheduleJob> pageDto= this.createJQPage(request);
 		List<ScheduleJob> jobList = scheduleJobService.queryList(pageDto,params,user);
 		int total = scheduleJobService.queryTotal(params);
 		pageDto.setRecords(jobList);
 		pageDto.setTotal(total);
 		return doSuccess(DataGridUtil.dataConvert(pageDto));
    }

    /**
     * 新增
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("addView")
    @RequiresPermissions("quartz:task:add")
    public ModelAndView addView(String menuId,ModelAndView modelAndView, HttpServletRequest request){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	modelAndView.setViewName("quartz/task/add");
        return modelAndView;
    }  
    
    /**
     * 保存
     * @param model
     * @param scheduleJob
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:add")
    public ResponseEntity<Result<Object>> add(Model model , ScheduleJob scheduleJob){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.save(scheduleJob, user);
            return doSuccess("保存成功");
        } catch (Exception e) {
           return doError("保存失败");
        }
    }
    
    /**
     * 修改
     * @param model
     * @param actModelEntity
     * @param request
     * @return
     */
    @RequestMapping("editView")
    @RequiresPermissions("quartz:task:add")
    public ModelAndView add(String menuId,Long id,ModelAndView modelAndView, HttpServletRequest request){
    	LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
	  	ScheduleJob scheduleJob = scheduleJobService.queryObject(id);
	  	modelAndView.addObject("record", scheduleJob);
	  	modelAndView.setViewName("quartz/task/edit");
        return modelAndView;
    }  
    
    /**
     * 修改
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:edit")
    public ResponseEntity<Result<Object>> edit(Model model , ScheduleJob scheduleJob){
    	LoginUser user = getLoginUser();
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.update(scheduleJob, user);
            return doSuccess("保存成功");
        } catch (Exception e) {
           return doError("保存失败");
        }
    }
    
    /**
     * 删除
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:delete")
    public ResponseEntity<Result<Object>> edit(Long id,Model model , ScheduleJob scheduleJob){
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.delete(id);
            return doSuccess("删除成功");
        } catch (Exception e) {
           return doError("删除失败");
        }
    }
    
    /**
     * 暂停
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "stop",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:stop")
    public ResponseEntity<Result<Object>> stop(Long id,Model model , ScheduleJob scheduleJob){
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.pause(id);
            return doSuccess("暂停成功");
        } catch (Exception e) {
           return doError("暂停失败");
        }
    }
    
    /**
     * 恢复
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "reBack",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:reBack")
    public ResponseEntity<Result<Object>> resume(Long id,Model model , ScheduleJob scheduleJob){
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.resume(id);
            return doSuccess("恢复成功");
        } catch (Exception e) {
           return doError("恢复失败");
        }
    }
    
    /**
     * 立即执行
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "start",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("quartz:task:start")
    public ResponseEntity<Result<Object>> start(Long id,Model model , ScheduleJob scheduleJob){
        ValidatorUtils.validateEntity(scheduleJob);
        try{
            scheduleJobService.run(id);
            return doSuccess("启动成功");
        } catch (Exception e) {
           return doError("启动失败");
        }
    }
	
	

}
